package hikari.objects3d;

import com.gooddies.wiring.Wiring;
import hikari.gui.panels.NavmeshEditorPanel;
import hikari.hcomponents.HComponent;
import hikari.persistence.file.FileSection;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.swig.jirr.ICustomizableTriangleMeshNode;
import org.swig.jirr.S3DVertex;
import org.swig.jirr.vector3df;

/**
 * @author Dmitry
 */
public class NavMesh3dObject extends Basic3dObject {
    public NavMesh3dObject(ICustomizableTriangleMeshNode node) {
        super("Navmesh", true);
        setMeshSceneNode(node);
    }

    @Override
    public ICustomizableTriangleMeshNode getMeshSceneNode() {
        return (ICustomizableTriangleMeshNode) super.getMeshSceneNode();
    }

    @Override
    public String objectType() {
        return "navmesh";
    }

    @Override
    public List<HComponent> getEditPanels() {
        List<HComponent> panels = super.getEditPanels();
        NavmeshEditorPanel editorPanel = Wiring.getComponent(NavmeshEditorPanel.class).updateFromModel(this);
        panels.add(editorPanel);
        return panels;
    }

    private long[] convertToLongArray(int[] array) {
        long[] newArray = new long[array.length];
        for (int i = 0; i < array.length; i++) {
            newArray[i] = array[i];
        }

        return newArray;
    }

    @Override
    public void writeData(FileSection parentSection) throws IOException {
        super.writeData(parentSection);
        FileSection s = parentSection.createNewSection("navmeshSettings");
        int[] indicies = getMeshSceneNode().getIndices();
        s.writeLongs("indicies", convertToLongArray(indicies));
        S3DVertex[] vertices = getMeshSceneNode().getVertices();
        vector3df[] positions = new vector3df[vertices.length];
        for (int i = 0; i < vertices.length; i++) {
            positions[i] = vertices[i].getPos();
        }

        s.writeVectors("vertices", positions);
        s.close();
    }
}
